package com.augurit.gzcgwsp.jzfqwczz.common.service.impl;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.directwebremoting.annotations.RemoteMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.gzcgwsp.jzfqwczz.common.convert.YwYnJzfqwczzXckctpConvertor;
import com.augurit.gzcgwsp.jzfqwczz.common.dao.YwYnJzfqwczzXckctpDao;
import com.augurit.gzcgwsp.jzfqwczz.common.entity.YwYnJzfqwczzXckctp;
import com.augurit.gzcgwsp.jzfqwczz.common.service.IYwYnJzfqwczzXckctpService;
import com.augurit.gzcgwsp.jzfqwczz.common.web.form.YwYnJzfqwczzXckctpForm;
import com.augurit.gzcgwsp.webservice.SFTPUtil;
import com.augurit.gzcgwsp.ywcmn.dwr.FileUploadCntl;
import com.augurit.gzcgwsp.ywcmn.entity.YwComSjcl;
@Service
@Transactional
public class YwYnJzfqwczzXckctpServiceImpl implements IYwYnJzfqwczzXckctpService{
    private static final Log log=LogFactory.getLog(YwYnJzfqwczzXckctpServiceImpl.class);
	@Autowired
	private YwYnJzfqwczzXckctpDao ywYnJzfqwczcXckctpDao;
	
	@Transactional(readOnly = true)
	public YwYnJzfqwczzXckctpForm get(Long id) {
		YwYnJzfqwczzXckctp entity = ywYnJzfqwczcXckctpDao.get(id);
		return YwYnJzfqwczzXckctpConvertor.convertVoToForm(entity);
	}

	public Page<YwYnJzfqwczzXckctpForm> search(
			Page<YwYnJzfqwczzXckctpForm> page, List<PropertyFilter> filters) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page<YwYnJzfqwczzXckctpForm> search(
			Page<YwYnJzfqwczzXckctpForm> page, YwYnJzfqwczzXckctpForm form) {
		// TODO Auto-generated method stub
		return null;
	}

	public void delete(Long... ids) {
		// TODO Auto-generated method stub
		
	}

	public void save(YwYnJzfqwczzXckctpForm... forms) {
		// TODO Auto-generated method stub
		
	}
	
	public void save(YwYnJzfqwczzXckctpForm form){
		
		if(form != null){
			YwYnJzfqwczzXckctp entity = null;
			
			if(form != null && form.getId() != null){
				entity = ywYnJzfqwczcXckctpDao.getEntity(form.getId());
				if(entity.getId()==null){
					entity = new YwYnJzfqwczzXckctp();
					entity.setId(form.getId());
				}
			}else{
				entity = new YwYnJzfqwczzXckctp();
			}
			
			YwYnJzfqwczzXckctpConvertor.convertFormToVo(form, entity);
			ywYnJzfqwczcXckctpDao.save(entity);
			
			form.setId(entity.getId());
		}
	}
	
	/**
	 * 余泥处-现场勘察图片  上传
	 * @param ajid 案件ID
	 * @param templateCode 业务模板编号
	 * @param uploadFile 流
	 * @throws Exception
	 */
	public YwYnJzfqwczzXckctpForm uploadXckcImage(String targetFilePath,long ajid, String templateCode,String fileOldName,String filetype, InputStream uploadFile)throws Exception{
		// 随机生成文件名后缀
		int bytesum = 0;
		int byteread = 0;
		String formatDate = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		int random = new Random().nextInt(1000000);
		String name = formatDate+random+"."+filetype;
		String address = "/resources/AttacheFile/";
//		String address2 = "F:/guowei/install/apache-tomcat-6.0.18/Tomcat 6.0/webapps/gzcgwsp/resources/AttacheFile/";
		FileOutputStream fs = new FileOutputStream(targetFilePath+name);
		
//		String sql = "update YwYnJzfqwczcJbxx jz set jz."+fjFlag+"='"+address+name+"', jz."+fjFlag+"name='"+fjOldName+"',jz."+fjFlag+"type='"+fjType+"' where jz.id="+ajid;
//		String sql = "update  YwYnJzfqwczcXckctp tp set tp.ajid='"+ajid+"',tp.templateCode='"+templateCode+"',tp.filetype='"+fjType+"',tp.filename='"+fjOldName+"',tp.filepath='"+address+name+"'";
//		System.out.println("sql = "+sql);
		YwYnJzfqwczzXckctpForm ywYnJzfqwczcXckctpForm = new YwYnJzfqwczzXckctpForm();
		ywYnJzfqwczcXckctpForm.setAjid(ajid);
		ywYnJzfqwczcXckctpForm.setTemplateCode(templateCode);
		ywYnJzfqwczcXckctpForm.setFiletype(filetype);
		ywYnJzfqwczcXckctpForm.setFilename(fileOldName);
		ywYnJzfqwczcXckctpForm.setFilepath(address+name);
		try{
			byte[] buffer = new byte[1024];
			while ((byteread = uploadFile.read(buffer)) != -1) {
				bytesum += byteread; // 字节数 文件大小
				fs.write(buffer, 0, byteread);
			}
			fs.flush();
			fs.close();
			uploadFile.close();
			log.info("--------开始保存");
			this.save(ywYnJzfqwczcXckctpForm);
			log.info("--------上传完成");
		}catch(Exception e){
			log.error("--------"+e.getMessage());
			e.printStackTrace();
		}
		  //上传附件到另外一台服务器
		log.info("--------开始上传另外一台服务器");
    	SFTPUtil.uploadOtherFile(address, targetFilePath+name);  
    	log.info("--------另外一台服务器上传完成");
		return ywYnJzfqwczcXckctpForm;
	}
	
	/**
	 * 获取余泥处-现场勘察图片
	 * @param ajid 案件ID
	 * @param templateCode 业务模板编号
	 * @throws Exception
	 */
	public List getImages(long ajid,String templateCode) throws Exception{
		String hql = "from YwYnJzfqwczzXckctp tp where tp.ajid='"+ajid+"' and tp.templateCode='"+templateCode+"' order by tp.id asc";
		List entityList = ywYnJzfqwczcXckctpDao.find(hql);
		return entityList;
	}
	
	/**
	 * 删除余泥处-现场勘察图片
	 * @param imageId ID
	 * @throws Exception
	 */
	public void deleteImageById(long imageId) throws Exception{
		ywYnJzfqwczcXckctpDao.delete(imageId);
	}

}
